<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
/**
 * Model for movie
 *
 * @author Matthijs van Henten  <Matthijs@waag.org>
 * @license GNU GPL 2.0
 * @verson 0.1
 * @copyright Copyright (c) 2009, Waag Society
 */
class Default_Model_Movie extends At_Model{
	const DEFAULT_OFFSET = 0;
    const DEFAULT_LIMIT = 10;

	// user data
	protected $_id;
	protected $_title;
	protected $_subject;
	protected $_creator;
	protected $_description;
	protected $_publisher;
	protected $_contributor;
	protected $_date;
	protected $_language;
	protected $_rights;
	protected $_filename;
	protected $_posterFrame;
	protected $_updated;
	protected $_inserted;

	// class properties
    protected $limit;
    protected $offset;

    protected $count;
    protected $channels;
	protected $channel;

	protected $info;

	public function getInfo(){
		if( null == $this->info && ( $filename = $this->__get('filename') ) && !empty( $filename ) ){
			$info = new Default_Model_VideoInfo();

			$info->setMovie( $this );
			$this->info = $info;
		}
		return $this->info;
	}

    /**
     * All the channels associated with this movie
     * @return array $channels
     */
    public function getChannels(){
		if( null !== $this->_id && null === $this->channels ){
			$channel = new Default_Model_Channel();
			$channel->setMovie( $this );

			$this->channels = $channel->fetchAll();
		}

		return $this->channels;
    }

	/**
	 * Set the channel ID's for this movie. This triggers an immediate update
	 * of the database!
	 *
	 * @param array $channels Array of channels OR channelids
	 * @return $this Fluid interface
	 */
	public function setChannels( $channels = array() ){
		$this->channels = array();

		foreach( $channels as $channel ){
			if( ! ( $channel instanceof Default_Model_Channel ) ){
				$channel = new Default_Model_Channel( intval( $channel ) );
			}

			if( null !== $channel->name ){
				$this->channels[] = $channel;
			}
		}

		$this->getMapper()->setChannels( $this );
		return $this;
	}

    /**
	 * Set a channel for this movie. If no ID is defined, fetchAll uses the channelId
	 * to select movies that have this channel
	 *
     * @param Default_Model_Channel $channel Channel to limit results for
     * @return $this Fluent interface
     */
    public function setChannel( Default_Model_Channel $channel ){
        $this->channel = $channel;
    }

	/**
	 * Return the channel set by setChannel or NULL
	 * @return Default_Model_Channel $channel OR NULL
	 */
    public function getChannel(){
        if( null !== $this->channel ){
            return $this->channel;
        }
    }

    /**
     * The current limit set for pager queries
     * @return int $limit
     */
    public function getLimit(){
        if( null === $this->limit ){
            $this->setLimit( self::DEFAULT_LIMIT );
        }

        return $this->limit;
    }

    /**
	 * Do a count query for the current resultset. use in pager-queries with
	 * offset, limit and fetchAll
     *
     * @return int $count;
     */
    public function getCount(){
        if( null === $this->count ){
            $this->count = $this->getMapper()->getCount( $this );
        }
        return $this->count;
    }

	/**
	 * Override the default offset( wich is 0 ). This is used for pager queries
	 * ( fetchAll uses offset and limit )
	 *
	 * @param int $offset Offset for the fetchAll query
	 * @return Default_Model_Movie $this movie, fluent interface
	 */
    public function setOffset( $offset ){
		if( null !== $offset && is_numeric( $offset ) ){
	        $this->offset = (int) $offset;
		}
        return $this;
    }

	/**
	 * Override the default limit as a class property ( which is 0 ). This is used
	 * in pager queries ( fetchAll uses offset and limit )
	 *
	 * @param int $limit Integer limit
	 * @return Default_Model_Movie $this movie, fluent interface
	 */
    public function setLimit( $limit ){
		if( null !== $limit && is_numeric( $limit ) ){
	        $this->limit = (int) $limit;
		}
        return $this;
    }

	/**
	 * Fetch the offset. if ofsset is not initlalized, self::DEFAULT_OFFSET is used
	 *
	 * @return int $offset
	 */
    public function getOffset(){
        if( null === $this->offset ){
            $this->setOffset( self::DEFAULT_OFFSET );
        }
        return $this->offset;
    }
}
